<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item label="客户" prop="customerName">
          <el-input
            v-model="queryParams.customerName"
            placeholder="评价客户昵称"
            clearable
            @keyup.enter="handleQuery()"
          />
        </el-form-item>
        <el-form-item label="客服" prop="agentName">
          <el-input
            v-model="queryParams.agentName"
            placeholder="客服"
            clearable
            @keyup.enter="handleQuery()"
          />
        </el-form-item>
        <el-form-item label="关键字" prop="feedbackContent">
          <el-input
            v-model="queryParams.feedbackContent"
            placeholder="文字评价内容"
            clearable
            @keyup.enter="handleQuery()"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <template #icon>
              <Search />
            </template>
            搜索
          </el-button>
          <el-button @click="handleResetQuery">
            <template #icon>
              <Refresh />
            </template>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never">
      <div class="mb-10px">
        <!--        <el-button-->
        <!--          v-hasPerm="['chat:customer-feedback:add']"-->
        <!--          type="success"-->
        <!--          @click="handleOpenDialog()"-->
        <!--        >-->
        <!--          <template #icon>-->
        <!--            <Plus />-->
        <!--          </template>-->
        <!--          新增-->
        <!--        </el-button>-->
        <el-button
          v-hasPerm="['chat:customer-feedback:delete']"
          type="danger"
          :disabled="removeIds.length === 0"
          @click="handleDelete()"
        >
          <template #icon>
            <Delete />
          </template>
          删除
        </el-button>
      </div>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="pageData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <!--        <el-table-column type="selection" width="55" align="center" />-->
        <el-table-column
          key="customerName"
          label="客户"
          prop="customerName"
          min-width="120"
          align="center"
        />
        <el-table-column
          key="agentName"
          label="客服"
          prop="agentName"
          min-width="90"
          align="center"
        />
        <el-table-column
          key="overallRating"
          label="总体评分"
          prop="overallRating"
          min-width="120"
          align="center"
        >
          <template #default="scope">
            <span :class="getScoreClass(scope.row.overallRating)">
              {{ scope.row.overallRating }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          key="responseSpeed"
          label="响应速度"
          prop="responseSpeed"
          min-width="120"
          align="center"
        >
          <template #default="scope">
            <span :class="getScoreClass(scope.row.responseSpeed)">
              {{ scope.row.responseSpeed }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          key="professionalism"
          label="专业程度"
          prop="professionalism"
          min-width="120"
          align="center"
        >
          <template #default="scope">
            <span :class="getScoreClass(scope.row.professionalism)">
              {{ scope.row.professionalism }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          key="serviceAttitude"
          label="服务态度"
          prop="serviceAttitude"
          min-width="120"
          align="center"
        >
          <template #default="scope">
            <span :class="getScoreClass(scope.row.serviceAttitude)">
              {{ scope.row.serviceAttitude }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          key="feedbackContent"
          label="文字评价内容"
          prop="feedbackContent"
          min-width="150"
          align="center"
        >
          <template #default="scope">
            <el-tooltip
              :content="scope.row.feedbackContent"
              placement="top"
              :show-after="500"
              :hide-after="2000"
            >
              <div class="feedback-content">{{ scope.row.feedbackContent }}</div>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column
          key="createTime"
          label="评价时间"
          prop="createTime"
          min-width="200"
          align="center"
        />
        <el-table-column
          key="sessionId"
          label="会话ID"
          prop="sessionId"
          min-width="80"
          align="center"
        />
        <!--        <el-table-column fixed="right" label="操作" width="220">-->
        <!--          <template #default="scope">-->
        <!--            <el-button-->
        <!--              v-hasPerm="['chat:customer-feedback:edit']"-->
        <!--              type="primary"-->
        <!--              size="small"-->
        <!--              link-->
        <!--              @click="handleOpenDialog(scope.row.id)"-->
        <!--            >-->
        <!--              <template #icon>-->
        <!--                <Edit />-->
        <!--              </template>-->
        <!--              编辑-->
        <!--            </el-button>-->
        <!--            <el-button-->
        <!--              v-hasPerm="['chat:customer-feedback:delete']"-->
        <!--              type="danger"-->
        <!--              size="small"-->
        <!--              link-->
        <!--              @click="handleDelete(scope.row.id)"-->
        <!--            >-->
        <!--              <template #icon>-->
        <!--                <Delete />-->
        <!--              </template>-->
        <!--              删除-->
        <!--            </el-button>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery()"
      />
    </el-card>

    <!-- 评价反馈表单弹窗 -->
    <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      width="500px"
      @close="handleCloseDialog"
    >
      <el-form ref="dataFormRef" :model="formData" :rules="rules" label-width="100px">
        <el-form-item label="" prop="id">
          <el-input v-model="formData.id" placeholder="" />
        </el-form-item>

        <el-form-item label="关联的会话ID" prop="sessionId">
          <el-input v-model="formData.sessionId" placeholder="关联的会话ID" />
        </el-form-item>

        <el-form-item label="评价客户ID" prop="customerId">
          <el-input v-model="formData.customerId" placeholder="评价客户ID" />
        </el-form-item>

        <el-form-item label="评价客户昵称" prop="customerName">
          <el-input v-model="formData.customerName" placeholder="评价客户昵称" />
        </el-form-item>

        <el-form-item label="客服人员ID" prop="agentId">
          <el-input v-model="formData.agentId" placeholder="客服人员ID" />
        </el-form-item>

        <el-form-item label="客服人员姓名" prop="agentName">
          <el-input v-model="formData.agentName" placeholder="客服人员姓名" />
        </el-form-item>

        <el-form-item label="总体评分(1-5星)" prop="overallRating">
          <el-input v-model="formData.overallRating" placeholder="总体评分(1-5星)" />
        </el-form-item>

        <el-form-item label="响应速度评分(1-5)" prop="responseSpeed">
          <el-input v-model="formData.responseSpeed" placeholder="响应速度评分(1-5)" />
        </el-form-item>

        <el-form-item label="专业程度评分(1-5)" prop="professionalism">
          <el-input v-model="formData.professionalism" placeholder="专业程度评分(1-5)" />
        </el-form-item>

        <el-form-item label="服务态度评分(1-5)" prop="serviceAttitude">
          <el-input v-model="formData.serviceAttitude" placeholder="服务态度评分(1-5)" />
        </el-form-item>

        <el-form-item label="问题解决程度(1-完全解决,2-部分解决,3-未解决)" prop="problemSolved">
          <el-input
            v-model="formData.problemSolved"
            placeholder="问题解决程度(1-完全解决,2-部分解决,3-未解决)"
          />
        </el-form-item>

        <el-form-item label="文字评价内容" prop="feedbackContent">
          <el-input v-model="formData.feedbackContent" placeholder="文字评价内容" />
        </el-form-item>

        <el-form-item label="评价标签(逗号分隔)" prop="feedbackTags">
          <el-input v-model="formData.feedbackTags" placeholder="评价标签(逗号分隔)" />
        </el-form-item>

        <el-form-item label="情感评价(positive/neutral/negative)" prop="emotionRating">
          <el-input
            v-model="formData.emotionRating"
            placeholder="情感评价(positive/neutral/negative)"
          />
        </el-form-item>

        <el-form-item label="是否匿名评价(0-否 1-是)" prop="isAnonymous">
          <el-input v-model="formData.isAnonymous" placeholder="是否匿名评价(0-否 1-是)" />
        </el-form-item>

        <el-form-item label="客服是否已回复(0-否 1-是)" prop="isReplied">
          <el-input v-model="formData.isReplied" placeholder="客服是否已回复(0-否 1-是)" />
        </el-form-item>

        <el-form-item label="逻辑删除标识(0-未删除 1-已删除)" prop="isDeleted">
          <el-input v-model="formData.isDeleted" placeholder="逻辑删除标识(0-未删除 1-已删除)" />
        </el-form-item>

        <el-form-item label="记录创建时间" prop="createTime">
          <el-date-picker
            v-model="formData.createTime"
            type="datetime"
            placeholder="记录创建时间"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
        </el-form-item>

        <el-form-item label="记录更新时间" prop="updateTime">
          <el-date-picker
            v-model="formData.updateTime"
            type="datetime"
            placeholder="记录更新时间"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit()">确定</el-button>
          <el-button @click="handleCloseDialog()">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
defineOptions({
  name: "CustomerFeedback",
  inheritAttrs: false,
});

import CustomerFeedbackAPI from "@/api/customer-feedback.js";

const queryFormRef = ref();
const dataFormRef = ref();

const loading = ref(false);
const removeIds = ref([]);
const total = ref(0);

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
});

// 评价反馈表格数据
const pageData = ref([]);

// 弹窗
const dialog = reactive({
  title: "",
  visible: false,
});

// 评价反馈表单数据
const formData = reactive({});

// 评价反馈表单校验规则
const rules = reactive({
  id: [
    {
      required: true,
      message: "请输入",
      trigger: "blur",
    },
  ],
});

/** 查询评价反馈 */
function handleQuery() {
  loading.value = true;
  CustomerFeedbackAPI.getPage(queryParams)
    .then((data) => {
      pageData.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置评价反馈查询 */
function handleResetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

/** 行复选框选中记录选中ID集合 */
function handleSelectionChange(selection) {
  removeIds.value = selection.map((item) => item.id);
}

/** 打开评价反馈弹窗 */
function handleOpenDialog(id) {
  dialog.visible = true;
  if (id) {
    dialog.title = "修改评价反馈";
    CustomerFeedbackAPI.getFormData(id).then((data) => {
      Object.assign(formData, data);
    });
  } else {
    dialog.title = "新增评价反馈";
  }
}

/** 提交评价反馈表单 */
function handleSubmit() {
  dataFormRef.value.validate((valid) => {
    if (valid) {
      loading.value = true;
      const id = formData.id;
      if (id) {
        CustomerFeedbackAPI.update(id, formData)
          .then(() => {
            ElMessage.success("修改成功");
            handleCloseDialog();
            handleResetQuery();
          })
          .finally(() => (loading.value = false));
      } else {
        CustomerFeedbackAPI.add(formData)
          .then(() => {
            ElMessage.success("新增成功");
            handleCloseDialog();
            handleResetQuery();
          })
          .finally(() => (loading.value = false));
      }
    }
  });
}

/** 关闭评价反馈弹窗 */
function handleCloseDialog() {
  dialog.visible = false;
  dataFormRef.value.resetFields();
  dataFormRef.value.clearValidate();
  formData.id = undefined;
}

/** 删除评价反馈 */
function handleDelete(id) {
  const ids = [id || removeIds.value].join(",");
  if (!ids) {
    ElMessage.warning("请勾选删除项");
    return;
  }

  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(
    () => {
      loading.value = true;
      CustomerFeedbackAPI.deleteByIds(ids)
        .then(() => {
          ElMessage.success("删除成功");
          handleResetQuery();
        })
        .finally(() => (loading.value = false));
    },
    () => {
      ElMessage.info("已取消删除");
    }
  );
}

// 根据分数返回对应的样式类
function getScoreClass(score) {
  if (!score) return "";
  const num = Number(score);
  if (num >= 4) return "score-high";
  if (num >= 3) return "score-medium";
  return "score-low";
}

onMounted(() => {
  handleQuery();
});
</script>

<style scoped>
.score-high {
  color: #67c23a;
  font-weight: bold;
}
.score-medium {
  color: #e6a23c;
  font-weight: bold;
}
.score-low {
  color: #f56c6c;
  font-weight: bold;
}
.feedback-content {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
  text-overflow: ellipsis;
  word-break: break-all;
  line-height: 1.5;
  max-height: 3em;
  cursor: pointer;
}
</style>
